<!doctype html>
<html itemscope itemtype="http://schema.org/WebPage" lang="zh-cn" class="no-js">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="generator" content="Hugo 0.121.1">
<link rel="canonical" type="text/html" href="/docs/contribution-guidelines/module-controller/">
<meta name="robots" content="noindex, nofollow">


<link rel="shortcut icon" href="/favicons/favicon.ico" >
<link rel="apple-touch-icon" href="/favicons/apple-touch-icon-180x180.png" sizes="180x180">
<link rel="icon" type="image/png" href="/favicons/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicons/android-36x36.png" sizes="36x36">
<link rel="icon" type="image/png" href="/favicons/android-48x48.png" sizes="48x48">
<link rel="icon" type="image/png" href="/favicons/android-72x72.png" sizes="72x72">
<link rel="icon" type="image/png" href="/favicons/android-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicons/android-144x144.png" sizes="144x144">
<link rel="icon" type="image/png" href="/favicons/android-192x192.png" sizes="192x192">

<title>ModuleController 技术文档 | SOFAServerless</title>
<meta name="description" content="">
<meta property="og:title" content="ModuleController 技术文档" />
<meta property="og:description" content="" />
<meta property="og:type" content="website" />
<meta property="og:url" content="/docs/contribution-guidelines/module-controller/" />

<meta itemprop="name" content="ModuleController 技术文档">
<meta itemprop="description" content=""><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="ModuleController 技术文档"/>
<meta name="twitter:description" content=""/>




<link rel="preload" href="/scss/main.min.526354c4efc1a4747972ed64c3be00f7db62aec5f0a3e6ae42052f97e2675d86.css" as="style">
<link href="/scss/main.min.526354c4efc1a4747972ed64c3be00f7db62aec5f0a3e6ae42052f97e2675d86.css" rel="stylesheet" integrity="">

<script
  src="https://code.jquery.com/jquery-3.6.3.min.js"
  integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ=="
  crossorigin="anonymous"></script>
<link rel="stylesheet" href="/css/prism.css"/>
<link href="/img/logo.svg" rel="icon" type="image/svg">
<link href="/search/pagefind-ui.css" rel="stylesheet">
<script src="/search/pagefind-ui.js" type="text/javascript"></script>

<script>
    window.addEventListener('DOMContentLoaded', (event) => {
        new PagefindUI({ element: ".td-search" });
    });
</script>


<script>
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'G-DZ8Q3F0GZ7', 'auto');
	
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>

<script async src="https://www.googletagmanager.com/gtag/js?id=G-DZ8Q3F0GZ7"></script>
<script>
var doNotTrack = false;
if (!doNotTrack) {
	window.dataLayer = window.dataLayer || [];
	function gtag(){dataLayer.push(arguments);}
	gtag('js', new Date());
	gtag('config', 'G-DZ8Q3F0GZ7');
}
</script>
  </head>
  <body class="td-section">
    <header>
      <nav class="td-navbar navbar-dark js-navbar-scroll">
<div class="container-fluid flex-column flex-md-row">
  <a class="navbar-brand" href="/"><span class="navbar-brand__logo navbar-logo"><svg width="26" height="29" viewBox="0 0 26 29" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><linearGradient x1="52.6662558%" y1="95.7312514%" x2="35.7492678%" y2="11.0078657%" id="linearGradient-wxw43fh1xd-1"><stop stop-color="#9822e4" offset="0"/><stop stop-color="#e643fa" offset="86.0585504%"/><stop stop-color="#f876ff" offset="100%"/></linearGradient><path d="M15.9275871 1.54432572 15.936122 1.5594269 15.9445709 1.57457632 5.01248933 7.79418913 7.699 9.304l8.8412359-4.72420311.8668497-.49490764C17.4171462 4.0791454 17.4272394 4.07345885 17.4373647 4.06782987c1.474137-.81952243 3.3335154-.28885241 4.1530379 1.18528466L10.875 11.088l2.864 1.609 8.843271-5.6213813c1.1038914-.6292563 2.5021428-.23250173 3.1230817.88617614C25.8984381 8.30965565 25.9998668 8.70204694 25.9998668 9.10116416V19.9436637c0 1.3476373-1.002317 2.484932-2.3392704 2.6542897L22.9421516 22.6889619V10.4257286L20.457252 12.0257475 20.457758 23.6880152c0 1.2665471-1.0267393 2.2932864-2.2932864 2.2932864H17.59115L17.590252 13.8727475 15.106 15.473 15.1067564 27.0588825c0 1.0695936-.855616100000001 1.9366704-1.911072 1.9366704C12.8696714 28.9955529 12.5490782 28.9110348 12.2643947 28.7500365L1.63270516 23.3171586C.159863707 22.5645249-.423980046 20.7604209.328653641 19.2875794L.372104915 19.2057013 12.0490412 25.0797183 12.049 22.086 1.98952173 16.7675786C.51279311 15.9867912-.0513787599 14.1567129.729408593 12.6799843L.744416289 12.6519572.759717752 12.6240895 12.049 18.593 12.0490412 15.2566254 1.7391964 9.45898637C1.44132502 9.2850324 1.19527431 9.0328708 1.02697438 8.72907568c-.516688418-.93266477-.189466545-2.11320819.7308705-2.63681754C6.19445786 3.56812633 9.52191759 1.67502747 11.7402241.412961569L11.7628911.400065593c1.4660277-.834070345 3.3306257-.321767634 4.164696 1.144260127z" id="path-wxw43fh1xd-2"/><linearGradient x1="50.6099966%" y1="31.6743333%" x2="50.2419846%" y2="73.0702907%" id="linearGradient-wxw43fh1xd-4"><stop stop-color="#8200b5" offset="0"/><stop stop-color="#8200b5" stop-opacity="0" offset="100%"/></linearGradient><linearGradient x1="50.6028666%" y1="31.6743333%" x2="50.2391561%" y2="73.0702907%" id="linearGradient-wxw43fh1xd-5"><stop stop-color="#8200b5" offset="0"/><stop stop-color="#8200b5" stop-opacity="0" offset="100%"/></linearGradient><linearGradient x1="90.8235257%" y1="50%" x2="3.24041867%" y2="52.9415572%" id="linearGradient-wxw43fh1xd-6"><stop stop-color="#dd3ff7" stop-opacity="0" offset="0"/><stop stop-color="#ca23e4" offset="100%"/></linearGradient><linearGradient x1="74.3272776%" y1="35.8586031%" x2="19.6895996%" y2="19.3309621%" id="linearGradient-wxw43fh1xd-7"><stop stop-color="#b800d1" stop-opacity="0" offset="0"/><stop stop-color="#ae00c5" offset="100%"/></linearGradient><linearGradient x1="97.0070676%" y1="71.8003287%" x2="11.3661868%" y2="30.5044309%" id="linearGradient-wxw43fh1xd-8"><stop stop-color="#940ec7" offset="0"/><stop stop-color="#c435f0" stop-opacity="0" offset="100%"/></linearGradient></defs><g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><g id="首页" transform="translate(-24.000000, -18.000000)"><g id="编组-4" transform="translate(24.000133, 16.000000)"><g id="路径-6-copy-2" transform="translate(0.000000, 2.004447)"><mask id="mask-wxw43fh1xd-3" fill="#fff"><use xlink:href="#path-wxw43fh1xd-2"/></mask><use id="Mask" fill="url(#linearGradient-wxw43fh1xd-1)" fill-rule="nonzero" xlink:href="#path-wxw43fh1xd-2"/><path d="M12.0490412 15.2684078 13.03943 15.5167092C14.3279076 16.0478608 15.1686754 17.3038563 15.1686754 18.6975193V18.7421449v9.047164l-3.1196342-1.4070083V15.2684078z" id="Path-114-Copy-2" fill="url(#linearGradient-wxw43fh1xd-4)" opacity=".69047619" mask="url(#mask-wxw43fh1xd-3)"/><path d="M22.9421516 10.4227424 23.8594661 10.2542707C24.8889759 10.219744 25.7876765 10.946368 25.9694916 11.9602841L26.0763097 12.5559692 26.0617858 22.9064645l-3.1196342-1.4070082V10.4227424z" id="Path-114-Copy-2" fill="url(#linearGradient-wxw43fh1xd-5)" opacity=".69047619" mask="url(#mask-wxw43fh1xd-3)"/><polygon id="Path-41" fill="url(#linearGradient-wxw43fh1xd-6)" mask="url(#mask-wxw43fh1xd-3)" points="7.6326499 9.31647968 11.9297751 6.77228295 14.9765833 9.31647968 10.7681311 11.1500753"/><path d="M6.16852974 12.7326226C8.05429918 13.6181711 7.86496926 14.83948 8.18831704 15.006813 8.70089861 15.2720748 9.14143095 17.098905 9.86115602 17.1695013 10.6949216 17.2512836 11.6478081 10.8552079 10.3936906 10.4556578 9.48069361 10.1647855 7.32719679 9.49121213 3.93320016 8.43493769c-.63204609 1.86851711.11306377 3.30107871 2.23532958 4.29768491z" fill="url(#linearGradient-wxw43fh1xd-7)" mask="url(#mask-wxw43fh1xd-3)" transform="translate(7.357416, 12.802608) rotate(-347.000000) translate(-7.357416, -12.802608)"/><polygon id="Path-43" fill="url(#linearGradient-wxw43fh1xd-8)" mask="url(#mask-wxw43fh1xd-3)" points="12.0699788 18.5615749 6.27140769 15.1359473 5.30588708 19.0963147 12.0699788 22.1120507"/></g></g></g></g></svg></span><span class="navbar-brand__name">SOFAServerless</span></a>
  <div class="td-navbar-nav-scroll ms-md-auto" id="main_navbar">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="/home/"><span>首页</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link active" href="/docs/"><span>产品文档</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/blog/"><span>最新信息</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/community/"><span>参与社区</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="/user-cases/"><span>用户案例</span></a>
      </li>
      <li class="nav-item dropdown d-none d-lg-block">
        <div class="dropdown">
  <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">中文</a>
  <ul class="dropdown-menu">
    <li><a class="dropdown-item" href="/no/"></a></li>
    </ul>
</div></li>
      </ul>
  </div>
  <div class="d-none d-lg-block">
    <div class="td-search">
  <div class="td-search__icon"></div>
  <input type="search" class="td-search__input form-control td-search-input" placeholder="站内搜索…" aria-label="站内搜索…" autocomplete="off">
</div>

  </div>
</div>
</nav>
    </header>
    <div class="container-fluid td-outer">
      <div class="td-main">
        <div class="row flex-xl-nowrap">
          <main class="col-12 col-md-9 col-xl-8 ps-md-5" role="main">
            




<div class="td-content">
<div class="pageinfo pageinfo-primary d-print-none">
<p>
这是本节的多页打印视图。
<a href="#" onclick="print();return false;">点击此处打印</a>.
</p><p>
<a href="/docs/contribution-guidelines/module-controller/">返回本页常规视图</a>.
</p>
</div>



<h1 class="title">ModuleController 技术文档</h1>





    <ul>
    
  
  
  
  

  
    
    
	
<li>1: <a href="#pg-72b8455792d08674da639fa21b8e66ae">ModuleController 架构设计</a></li>


    
  
    
    
	
<li>2: <a href="#pg-be4b5c4491d910c089a03f3bd6b4ec7b">CRD 模型设计</a></li>


    
  
    
    
	
<li>3: <a href="#pg-339ad39102ec6d572f675d168eb0a6c2">核心代码结构</a></li>


    
  
    
    
	
<li>4: <a href="#pg-de7e40f637d5e11c931b8c9e41910f4d">模块生命周期</a></li>


    
  
    
    
	
<li>5: <a href="#pg-4c6a73428cd292db6a65b7e3740abdd4">核心流程时序图</a></li>


    
  

    </ul>


<div class="content">
      
</div>
</div>


  
  
  
  

  
  

  
    
    
	
    

<div class="td-content" style="">
    
	<h1 id="pg-72b8455792d08674da639fa21b8e66ae">1 - ModuleController 架构设计</h1>
    
	<h2 id="介绍">介绍</h2>
<p>ModuleController 是一个 K8S 控制器，该控制器参考 K8S 架构，定义并且实现了 ModuleDeployment、ModuleReplicaSet、Module 等核心模型与调和能力，从而实现了 Serverless 模块的秒级运维调度，以及与基座的联动运维能力。</p>
<h2 id="基本架构">基本架构</h2>
<p>ModuleController 目前包含 ModuleDeployment Opertor、ModuleReplicaSet Operator、Module Operator 三个组件。和 K8S 原生 Deployment 类似，<strong>用户创建 ModuleDeployment 会调和出 ModuleReplicaSet，ModuleReplicaSet 会进一步调和出 Module，最终 Module Operator 会调用 Pod 里的 Arklet SDK 去安装或卸载模块</strong>。此外 ModuleController 还会为 ModuleDeployment 自动生成 K8S Service，企业可以监听该 Service 的 IP 变化实现与自身流量控制系统的集成，从而实现模块粒度的切流和挂流。<br />
<a href="architecture.png"><img src="../architecture.png#from=url&amp;height=536&amp;id=ZnBYG&amp;originHeight=502&amp;originWidth=645&amp;originalType=binary&amp;ratio=2&amp;rotation=0&amp;showTitle=false&amp;status=done&amp;style=none&amp;title=&amp;width=689" alt=""></a></p>
<h2 id="功能清单和-roadmap">功能清单和 RoadMap</h2>
<ul>
<li><strong>08.15：0.2 版本</strong>上线（包括非对等模块发布、卸载、扩缩容、副本保持、基座运维联动）</li>
<li><strong>08.25：0.3 版本</strong>上线（包括回滚链路、各项参数校验、单测达到 80/60、CI 自动化、开发者指南）</li>
<li><strong>09.31：0.5 版本</strong>上线（1:1 先扩后缩、模块回滚、两种调度策略、状态回流、1+ 端到端集成测试）</li>
<li><strong>10.30：0.6 版本</strong>上线（支持以 K8S Service 方式联动企业四七层流量控制、总计 10+ 端到端集成测试）</li>
<li><strong>11.30：1.0 版本</strong>上线（支持对等发布运维、各项修复打磨、总计 20+ 端到端集成测试）</li>
<li><strong>12.30：1.1 版本</strong>上线（支持模块和基座自动弹性伸缩、对等与非对等发布运维能力完善）</li>
</ul>
<br/>

</div>



    
	
  
    
    
	
    

<div class="td-content" style="page-break-before: always">
    
	<h1 id="pg-be4b5c4491d910c089a03f3bd6b4ec7b">2 - CRD 模型设计</h1>
    
	<h2 id="crd-模型对比">CRD 模型对比</h2>
<table>
<thead>
<tr>
<th>K8S 原生 CRD</th>
<th>ModuleController CRD</th>
<th>关系和区别</th>
</tr>
</thead>
<tbody>
<tr>
<td>Pod</td>
<td>Module</td>
<td>Pod：K8S 中创建和管理的、最小的可部署的计算单元。     Module：Serverless 创建和管理的、最小的可部署的计算单元。</td>
</tr>
<tr>
<td>PodSpec</td>
<td>ModuleSpec</td>
<td>PodSpec：对 Pod 的描述。包含容器、调度、卷等。     ModuleSpec：对 Module 的描述，包含模块、服务、调度（亲和性）。</td>
</tr>
<tr>
<td>PodTemplate</td>
<td>ModuleTemplate</td>
<td>PodTemplate：定义 Pod 的生成副本，包含 PodSpec。     ModuleTemplate：定义 Module 的生成副本，包含 ModuleGroupSpec。</td>
</tr>
<tr>
<td>Deployment</td>
<td>ModuleDeployment</td>
<td>Deployment：定义 Pod 的期望状态和副本数量。     ModuleDeployment：定义 Module 的期望状态和副本数量。</td>
</tr>
<tr>
<td>ReplicaSet</td>
<td>ModuleReplicaSet</td>
<td>ReplicaSet：管理 Pod 的运行副本。     <br />ModuleReplicaSet：管理 Module 的运行副本。</td>
</tr>
</tbody>
</table>
<h2 id="moduledeployment-crd-模型">ModuleDeployment CRD 模型</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/863d8ede-4904-423e-9473-77466af33c46" alt="image"></p>
<h2 id="module-crd-模型">Module CRD 模型</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/f4e109eb-4b10-4835-a502-7d723b1ca73c" alt="image"></p>
<h2 id="moduletemplate-crd-模型">ModuleTemplate CRD 模型</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/db4fd36b-d698-4946-8d62-6e6651d3f18a" alt="image"></p>
<h2 id="modulereplicaset-crd-模型">ModuleReplicaSet CRD 模型</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/13fbf29e-3977-4138-b3dd-849ce871fb3b" alt="image"></p>
<br/>

</div>



    
	
  
    
    
	
    

<div class="td-content" style="page-break-before: always">
    
	<h1 id="pg-339ad39102ec6d572f675d168eb0a6c2">3 - 核心代码结构</h1>
    
	<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2023/png/671/1694002853387-dfb011bb-b443-401d-b6b2-0e2d89d9ca38.png#clientId=u9331e04a-ec01-4&amp;from=paste&amp;height=733&amp;id=u434d34f6&amp;originHeight=1466&amp;originWidth=2236&amp;originalType=binary&amp;ratio=2&amp;rotation=0&amp;showTitle=false&amp;size=746447&amp;status=done&amp;style=none&amp;taskId=u60bb866c-f80f-4cda-b668-d41bdef0f7b&amp;title=&amp;width=1118" alt="image.png"></p>
<p><img src="https://intranetproxy.alipay.com/skylark/lark/0/2023/png/671/1694002891414-adf2f622-38ec-46cb-8f73-4ae6efdd87ab.png#clientId=u9331e04a-ec01-4&amp;from=paste&amp;height=107&amp;id=u798a320c&amp;originHeight=214&amp;originWidth=612&amp;originalType=binary&amp;ratio=2&amp;rotation=0&amp;showTitle=false&amp;size=53871&amp;status=done&amp;style=none&amp;taskId=u47639d5b-c3d6-4356-800a-789e9f2791c&amp;title=&amp;width=306" alt="image.png"></p>
<br/>
<p>核心代码逻辑在 moduledeployment_controller.go、modulereplicaset_controller.go、module_controller.go、controller_utils.go，里面有详细注释。</p>
<br/>
<br/>

</div>



    
	
  
    
    
	
    

<div class="td-content" style="page-break-before: always">
    
	<h1 id="pg-de7e40f637d5e11c931b8c9e41910f4d">4 - 模块生命周期</h1>
    
	<h3 id="模块生命周期">模块生命周期</h3>
<p>4象限描述了模块的生命周期： Prepare、Upgrading、Completed、Available</p>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/16ec7808-eab9-4293-b9c2-cddda3de5d85" alt="image"></p>
<h3 id="模块状态机">模块状态机</h3>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/d06c10d6-1d37-48a9-9af3-0d44a6d7e1fd" alt="image"></p>
<br/>

</div>



    
	
  
    
    
	
    

<div class="td-content" style="page-break-before: always">
    
	<h1 id="pg-4c6a73428cd292db6a65b7e3740abdd4">5 - 核心流程时序图</h1>
    
	<h2 id="模块首发">模块首发</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/d449f851-d989-4221-8809-f39a786529af" alt="image"></p>
<h2 id="模块二发">模块二发</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/1f649f5e-b8bd-458c-ba9d-333ed1fad46c" alt="image"></p>
<h2 id="模块下线">模块下线</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/867c16a3-a3a8-4ace-b132-0051bd2ba0ad" alt="image"></p>
<h2 id="对等基座扩容">对等基座扩容</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/c0db1f13-0ec8-43b4-a24e-7b8677405a15" alt="image"></p>
<h2 id="对等基座缩容">对等基座缩容</h2>
<p><img src="https://github.com/sofastack/sofa-serverless/assets/13743483/7e950729-08dd-4264-aa25-0ddfa6828b79" alt="image">
<br/></p>

</div>



    
	
  



          </main>
        </div>
      </div>
      <footer class="td-footer row d-print-none">
  <div class="container-fluid">
    <div class="row mx-md-2">
      <div class="col-6 col-sm-4 text-xs-center order-sm-2">
        
        
        
<ul class="td-footer__links-list">
  
  <li class="td-footer__links-item" data-bs-toggle="tooltip" title="通过社区交流群" aria-label="通过社区交流群">
    <a target="_blank" rel="noopener" href="/docs/contribution-guidelines/communication-channel/" aria-label="通过社区交流群">
      <i class="fab fa-twitter"></i>
    </a>
  </li>
  
</ul>

        
        
      </div>
      <div class="col-6 col-sm-4 text-end text-xs-center order-sm-3">
        
        
        
<ul class="td-footer__links-list">
  
  <li class="td-footer__links-item" data-bs-toggle="tooltip" title="看这里" aria-label="看这里">
    <a target="_blank" rel="noopener" href="/docs/contribution-guidelines/contribution/first-pr/" aria-label="看这里">
      <i class="fab fa-github"></i>
    </a>
  </li>
  
</ul>

        
        
      </div>
      <div class="td-footer__copyright-etc col-12 col-sm-4 text-center py-2 order-sm-2">
        <span>&copy; 2023 SOFAServerless 开源社区 保留所有权利</span>
        
        
      </div>
    </div>
  </div>
</footer>
    </div>
    
  <script src="/js/main.min.1eb4262674b2d02aa8d18559fef13b166dbdfa627fd0a495c66e11577c026aa3.js" integrity="sha256-HrQmJnSy0Cqo0YVZ/vE7Fm29&#43;mJ/0KSVxm4RV3wCaqM=" crossorigin="anonymous"></script>
<script src='/js/prism.js'></script>
<script src='/js/tabpane-persist.js'></script>

  </body>
</html>
